<?php
/** 
 * 找出租
 * Author:zhang
 *
 */

namespace Api\Controller;

class RentController extends ApiController{
	public function __construct(){
    	parent::__construct();
    	/** 测试不判断登录 modify by zhang 2016 start  **/
    	if (!I('mytest')) {
	        !api_login() && response(-94);
	        !passed_login() && response(-95);
	        !only_login() && response(-96);
    	}
    	/** 测试不判断登录 modify by zhang 2016 end **/
	}
	// 我的出租信息
	function myrent(){
	    $uid = API_UID;
		$list = M('rent')->where(array('u_id'=>$uid))->select();
        foreach ($list as $key =>$value) {
            $cover_ids= explode(',', $value['litpic']);
            foreach ($cover_ids as $key1=>$value1) {
                $list[$key]['imgPath'][$key1] = M('picture')->where('id='.$value1)->getField('path');
            }
        }
		$list?response(1,$list):response(-9);
	}
    /**
     * 修改出租信息
     */
    public function edit() {
        $id = I('id');
        $data = M('rent')->create();
        $data['time'] = NOW_TIME;
        $result = M('rent')->where('id='.$id)->save($data);
        if ($result) {
            response(1);
        }else{
            response(-9);
        }
    }
    /**
     * 删除出租信息
     */
    public function delete() {
        $id = I('id');
        $result = M('rent')->where('id='.$id)->delete();
        $result?response(1):response(-9);
    }
	// 添加出租信息
	function add(){
	    $uid = API_UID;
        $today = strtotime(date("Y-m-d"));
        $num = M('rent')->where(array('time'=>array('gt',$today),'u_id'=>$uid))->count();
        if ($num >= 1) {
            $amount = M('member')->where('uid='.$uid)->getField('amount');
            if (!$amount) {
                $amount = 0;
            }
            if ($amount<2) {
                response(-2, '', '余额不足');
            }
            M('member')->where('uid='.$uid)->setDec('amount', '2');
        }
        $data = M('rent')->create();
        $data['u_id'] = $uid;
        $data['time'] = NOW_TIME;
        $result = M('rent')->add($data);
		if ($result) {
			response(1);
		}else{
			response(-9);
		}
	}
	//判断是否已经发布过租房信息
    public function hasRent() {
        $uid = API_UID;
        $today = strtotime(date("Y-m-d"));
        $num = M('rent')->where(array('time'=>array('gt',$today),'u_id'=>$uid))->count();
        if ($num>=1) {
            $data['hasRent'] = 1;
        } else {
            $data['hasRent'] = 0;
        }
        response(1, $data);
    }
	//详情
	function info(){
		params_exist(array('id'));
		$id = intval(I('id'));

		$info = M('rent')->alias('r')
				->field('r.*, u.mobile')
				->join('left join __UCENTER_MEMBER__ u on r.u_id=u.id')
				->where(array('r.id'=>$id))
				->find();
        $cover_ids= explode(',', $info['litpic']);
        foreach ($cover_ids as $key=>$value) {
            $info['imgPath'][$key] = M('picture')->where('id='.$value)->getField('path');
        }
		$info?response(1,$info):response(-9);
	}
	//找出租列表or搜索
	function rentList(){
		$where = array();
		if (I("post.qu")) {
			$where['qu'] = array('like',"%".I('post.qu')."%");
		}
		if (I('post.hall_room')) {
			$where['hall_room'] = I('post.hall_room');
		}
		if (I("post.price")) {
			$r = explode('-',$_POST['price']);
			foreach($r as $key=>$val) {
			    $arr[] = intval($val);
            }
			$where['price'] = array('between',$arr);
		}
		$list = M('rent')->where($where)->select();
		foreach ($list as $key =>$value) {
            $cover_ids= explode(',', $value['litpic']);
            foreach ($cover_ids as $key1=>$value1) {
                $list[$key]['imgPath'][$key1] = M('picture')->where('id='.$value1)->getField('path');
            }
        }
		$list?response(1,$list):response(-9);
	}
}